/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package session

import (
	"gitee.com/tugoer/config"
	"os"
	"path/filepath"
	"time"
)

var Env = &struct {
	// cookie key
	CookieKey  string `yaml:"cookie_key" json:"cookie_key"`

	// 过期时间 /minute
	ExpireTime int64  `yaml:"expire_time" json:"expire_time"` // per minute

	// 每次更新时随机检查过期数量
	CheckExpireNumEachUpdate int    `json:"check_expire_num_each_update" yaml:"check_expire_num_each_update"`

	// 默认存储器的存储目录
	SessionDir               string `json:"session_dir" yaml:"session_dir"`
}{
	CookieKey:                "orivil-session",
	ExpireTime:               45, // 默认45分钟
	CheckExpireNumEachUpdate: 3,  // 建议设置在 2-3
	SessionDir:               filepath.Join(os.TempDir(), "orivil-sessions"),
}

func init() {
	dir := "./configs"
	err := os.MkdirAll(dir, 0666)
	if err != nil {
		panic(err)
	}
	parser := config.NewJsonParser(dir)
	err = parser.ReadOrCreate("session", &Env)
	if err != nil {
		panic(err)
	}
	Env.ExpireTime = Env.ExpireTime * int64(time.Minute)
	DefaultStorage = NewFileStorage(Env.SessionDir)
	DefaultContainer = NewContainer(DefaultStorage)
}
